﻿using Groep2TINGHService;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1.Admin_View
{
    /// <summary>
    /// Interaction logic for Home.xaml
    /// </summary>
    public partial class Home : MetroWindow
    {
        //declaratie
        static string Username;
        static string Paswoord;
        static string PaswoordBev;
        static string Vraag;
        static string Antwoord;
        static string Straat;
        static string Postcode;
        static string Plaats;
        static string Land;
        static string Email;
        static string Telefoon;
        static int Medewerkers;
        static string Rol;

        public Home()
        {
            InitializeComponent();

            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Role role = new Role();
            List<Role> lijstRoles = myClient.Get_AllRoles();
            lijstRoles.Add(role);
            foreach (Role r in lijstRoles)
            {
                cmbRoles.Items.Add(r.RoleName);
            }

            Company company = new Company();
            List<Company> lijstCompanies = myClient.GetAllCompanies();
            lijstCompanies.Add(company);
            foreach (Company r in lijstCompanies)
            {
                lstBedrijven.Items.Add(r.Name);
            }

            foreach (Company r in lijstCompanies)
            {
                lstCompany.Items.Add(r.Name);
            }
            Contract contract = new Contract();
            List<Contract> LijstCompaniesStop = myClient.GetAllContracts();
            LijstCompaniesStop.Add(contract);
            foreach (Contract r in LijstCompaniesStop)
            {
                lstBedrijvenStopzetten.Items.Add(r.Id);
            }

            ServiceReference1.Service1Client myClient2 = new ServiceReference1.Service1Client();
            ContractFormula formula = new ContractFormula();
            List<ContractFormula> lijstFormula = myClient2.GetAllContractFormula();
            lijstFormula.Add(formula);
            foreach (ContractFormula r in lijstFormula)
            {
                cmbFormula.Items.Add(r.Description);
            }
            txtEinddatum.IsEnabled = false;
        }

        private void btnToevoegen_Click(object sender, RoutedEventArgs e)
        {
            Username = txtBedrijf.Text;
            Paswoord = txtPassword.Password;
            PaswoordBev = txtConfirmPw.Password;
            Vraag = txtVraag.Text;
            Antwoord = txtAntwoord.Password;
            Straat = txtStraat.Text;
            Postcode = txtPostcode.Text;
            Plaats = txtPlaats.Text;
            Land = txtLand.Text;
            Email = txtEmail.Text;
            Telefoon = txtTelefoon.Text;
            Medewerkers = Convert.ToInt32(txtMedewerkers.Text);
            Rol = cmbRoles.SelectedItem.ToString();
            MembershipCreateStatus result;
            if (Paswoord.Equals(PaswoordBev))
            {
                if (!(Membership.FindUsersByName(Username) == null))
                {
                    Membership.CreateUser(Username, Paswoord, Email, Vraag, Antwoord, true, out result);

                    ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
                    Company company = new Company();
                    company.Name = txtBedrijf.Text;
                    company.Street = txtStraat.Text;
                    company.ZipCode = Convert.ToInt32(txtPostcode.Text);
                    company.City = txtPlaats.Text;
                    company.Country = txtLand.Text;
                    company.Email = txtEmail.Text;
                    company.Phone = txtTelefoon.Text;
                    company.Employees = Convert.ToInt32(txtMedewerkers.Text);
                    myClient.Insert_Company(company);
                    ClearAll();

                    MessageBox.Show(result.ToString());
                }
                if (!Roles.RoleExists(Rol))
                {

                    Roles.CreateRole(Rol);
                }
                if (!Roles.IsUserInRole(Username, Rol))
                {
                    Roles.AddUserToRole(Username, Rol);
                }
            }
            else
            {
                MessageBox.Show("De paswoorden komen niet overeen.");
                ClearAll();
            }
            lstBedrijven.Items.Clear();
            ServiceReference1.Service1Client myClient2 = new ServiceReference1.Service1Client();
            Company company2 = new Company();
            List<Company> lijstCompanies = myClient2.GetAllCompanies();
            lijstCompanies.Add(company2);
            foreach (Company r in lijstCompanies)
            {
                lstBedrijven.Items.Add(r.Name);
            }
            lstCompany.Items.Clear();
            foreach (Company r in lijstCompanies)
            {
                lstCompany.Items.Add(r.Name);
            }

        }
        public void ClearAll()
        {
            txtBedrijf.Text = "";
            txtPassword.Password = "";
            txtConfirmPw.Password = "";
            txtVraag.Text = "";
            txtAntwoord.Password = "";
            txtStraat.Text = "";
            txtPostcode.Text = "";
            txtPlaats.Text = "";
            txtLand.Text = "";
            txtEmail.Text = "";
            txtTelefoon.Text = "";
            txtMedewerkers.Text = "";
            cmbRoles.SelectedIndex = -1;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Company company = new Company();
            company.Name = lstBedrijven.SelectedItem.ToString();
            company.Street = txtEditStraat.Text;
            company.ZipCode = Convert.ToInt32(txtEditPostcode.Text);
            company.City = txtEditPlaats.Text;
            company.Country = txtEditLand.Text;
            company.Email = txtEditEmail.Text;
            company.Phone = txtEditTelefoon.Text;
            company.Employees = Convert.ToInt32(txtEditMedewerkers.Text);
            myClient.Modify_Company(company);

            MessageBox.Show("Bedrijf aangepast!");




        }

        private void lstBedrijven_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Company company = new Company();
            if (lstBedrijven.Items.Count > 0)
            {
                string bedrijf = lstBedrijven.SelectedItem.ToString();
                company = myClient.Get_CompanyByName(bedrijf);
                txtEditStraat.Text = company.Street;
                txtEditPostcode.Text = company.ZipCode.ToString();
                txtEditPlaats.Text = company.City;
                txtEditLand.Text = company.Country;
                txtEditEmail.Text = company.Email;
                txtEditTelefoon.Text = company.Phone;
                txtEditMedewerkers.Text = company.Employees.ToString();
            }
        }

        private void btnVerwijder_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Company company = new Company();
            company.Name = lstBedrijven.SelectedItem.ToString();

            int x = myClient.DeleteCompany(company);
           
            if (x > 0)
            {
                MessageBox.Show("Bedrijf verwijdert!");
                lstBedrijven.Items.Clear();
                Company company2 = new Company();
                List<Company> lijstCompanies2 = myClient.GetAllCompanies();
                lijstCompanies2.Add(company2);
                foreach (Company r in lijstCompanies2)
                {
                    lstBedrijven.Items.Add(r.Name);
                }
                lstCompany.Items.Clear();
                foreach (Company r in lijstCompanies2)
                {
                    lstCompany.Items.Add(r.Name);
                }
                
            }
            else
            {
                MessageBox.Show("Bedrijf heeft nog contracten openstaan");
            }
        }
        private void btnBevestig_Click(object sender, RoutedEventArgs e)
        {
            //controle of datum juist is
            if (Convert.ToDateTime(dtpStartDate.SelectedDate) < DateTime.Today)
            {
                MessageBox.Show("Startdatum kan niet in het verleden zijn.");
            }
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();

            Contract contract = new Contract();
            ContractFormula formula = new ContractFormula();
            Company company = new Company();
            contract.StartDate = Convert.ToDateTime(dtpStartDate.SelectedDate);
            formula.Description = cmbFormula.SelectedItem.ToString();
            contract.EndDate = Convert.ToDateTime(txtEinddatum.Text);
            company.Name = lstCompany.SelectedItem.ToString();

            int x = myClient.InsertContract(contract, formula, company);
            if (x > 0)
            {
                MessageBox.Show("Er is een contract aangemaakt");
                lstBedrijvenStopzetten.Items.Clear();
                Contract contract2 = new Contract();
                List<Contract> LijstCompaniesStop = myClient.GetAllContracts();
                LijstCompaniesStop.Add(contract2);
                foreach (Contract r in LijstCompaniesStop)
                {
                    lstBedrijvenStopzetten.Items.Add(r.Id);
                }
            }
            else
            {
                MessageBox.Show("Er is een fout opgelopen zorg dat alle velden goed ingevuld zijn");
            }





        }

        private void cmbFormula_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            ContractFormula formula = new ContractFormula();
            formula.Description = cmbFormula.SelectedItem.ToString();
            ContractFormula f2 = new ContractFormula();
            f2 = myClient.Get_ContractDurationByDescription(formula);
            DateTime StartDatum = Convert.ToDateTime(dtpStartDate.SelectedDate);
            txtEinddatum.Text = Convert.ToString(StartDatum.AddMonths(Convert.ToInt32(f2.PeriodInMonths.ToString())));
        }

        private void lstBedrijvenStopzetten_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstBedrijvenStopzetten.Items.Count > 0) { 
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            string contractID = lstBedrijvenStopzetten.SelectedItem.ToString();
            Company company = new Company();
            ContractFormula formula = new ContractFormula();
            Contract contract = new Contract();

            contract = myClient.GetContractByCompany(contractID);
            txtStartDatumStop.Text = contract.StartDate.ToString();
            txtEindDatumStop.Text = contract.EndDate.ToString();

            company = myClient.GetContractByCompanyWithName(contractID);
            txtBedrijfsnaam.Text = company.Name;

            formula = myClient.GetContractByCompanyWithDescription(contractID);
            txtFormulaStop.Text = formula.Description;
            }
        }

        private void btnVerwijderen_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            string contractID = lstBedrijvenStopzetten.SelectedItem.ToString();
            Contract contract = new Contract();
            int x = myClient.DeleteContract(contractID);
            if (x > 0)
            {
                lstBedrijvenStopzetten.Items.Clear();
                ServiceReference1.Service1Client myClient2 = new ServiceReference1.Service1Client();
                Contract contract2 = new Contract();
                List<Contract> LijstCompaniesStop = myClient2.GetAllContracts();
                LijstCompaniesStop.Add(contract2);
                foreach (Contract r in LijstCompaniesStop)
                {
                    lstBedrijvenStopzetten.Items.Add(r.Id);
                }
                MessageBox.Show("Contract verwijdert!");
            }


        }

        private void txtEinddatum_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void lstCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            Login login = new Login();
            login.Show();
            this.Close();
        }
    }
}
